/**
 * @Description: 文本框验证规则
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/6
 */
/**
 * @Author: Mr.Yan
 * @date: 2019/1/9
 * @Description: 文本框改变移除class
 */
$(function () {
    $(".layui-input").on('focus', function () {
        $(this).removeClass("layui-form-green");
        $(this).removeClass("layui-form-danger");
    });
});
/**
 * @Author: Mr.Yan
 * @date: 2019/1/9
 * @Description: 表单验证
 */
function validate_form(thisform) {
    var isSuccess = true;
    $(thisform).find("input").removeClass("layui-form-green");
    $(thisform).find("input").removeClass("layui-form-danger");
    $(thisform).find("select").removeClass("layui-form-green");
    $(thisform).find("select").removeClass("layui-form-danger");
    for (var i = 0; i < thisform.length; i++) {
        var pro = $(thisform[i]);
        //验证规则
        var validateName = pro.attr("validate");
        if(!isEmpty(validateName)){
            names = validateName.split("|");
            var boole = false;
            for(var j=0;j<names.length;j++){
                if(!isEmpty(names[j])){
                    if(!validate(pro,names[j])){
                        pro.removeClass("layui-form-green");
                        pro.addClass("layui-form-danger");
                        boole = true;
                        break
                    }else {
                        pro.removeClass("layui-form-danger");
                        pro.addClass("layui-form-green");
                    }
                }
            }
            if(boole){
                isSuccess = false;
                return isSuccess;
                break;
            }
        }
    }
    return isSuccess;
}

//  验证规则
var  validate =  function (e,name){
    switch (name) {
        case "required" :
            return required(e);
            break;
        case "loginName" :
            return loginName(e);
            break;
        case "passWord" :
            return passWord(e);
            break;
        case "telphone" :
            return telphone(e);
            break;
        case "email" :
            return email(e);
            break;
    }


}

//  必填项
function required(e) {
    var reg =  /[\S]+/;
    if(reg.test(e.val())){
        return true;
    }else{
        layTool.showMsg("必填项");
        return false;
    }
}

//  验证登录账号
function loginName(e) {
    var reg = /^[a-zA-Z][a-zA-Z0-9]{4,10}$/;
    if(isEmpty(e.val())){
        return true;
    }
    if(reg.test(e.val())){
        return true;
    }else{
        layTool.showMsg("以字母开头,4到10个字母或数字组成");
        return false;
    }
}

//  验证密码
function passWord(e) {
    var reg = /^[A-Z][0-9A-Za-z]{4,}$/;
    if(isEmpty(e.val())){
        return true;
    }
    if(reg.test(e.val())){
        return true;
    }else{
        layTool.showMsg("以大写字母开头,4位以上个字母或数字组成");
        return false;
    }
}
//  验证手机号
function telphone(e) {
    var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(isEmpty(e.val())){
        return true;
    }
    if(reg.test(e.val())){
        return true;
    }else{
        layTool.showMsg("手机号格式不正确");
        return false;
    }
}

//  验证邮箱
function email(e) {
    var reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    if(isEmpty(e.val())){
        return true;
    }
    if(reg.test(e.val())){
        return true;
    }else{
        layTool.showMsg("邮箱格式不正确");
        return false;
    }
}

//_______________________________

// 判断是否为空
function isEmpty(str) {
    if(str==null || str=='' || str== undefined){
        return true;
    }else{
        return false;
    }
}

